package mil.af.amc.adpetracker.web.actions.ItemModel;

import mil.af.amc.adpetracker.model.ItemModel;
import mil.af.amc.adpetracker.service.ItemModelService;
import mil.af.amc.adpetracker.web.actions.AbstractDispatchAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Dispatch Action to hand ItemModel actions
 *
 * @author TSgt Jason Ferguson
 */

public class ItemModelDispatchAction extends AbstractDispatchAction {

    Log log = LogFactory.getLog(ItemModelDispatchAction.class);

    private ItemModelService itemModelService;

    public void setItemModelService(ItemModelService itemModelService) {
        this.itemModelService = itemModelService;
    }

    public ActionForward viewModelIndex(ActionMapping actionMapping,
                                         ActionForm actionForm,
                                         HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewModelIndex");

    }

    public ActionForward viewCreateModelPopup(ActionMapping actionMapping,
                                         ActionForm actionForm,
                                         HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse) {


        return actionMapping.findForward("viewCreateModelPopup");
    }

    public ActionForward viewCreateModel(ActionMapping actionMapping,
                                         ActionForm actionForm,
                                         HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse) {

        return actionMapping.findForward("viewCreateModel");

    }

    public ActionForward createItemModel(ActionMapping actionMapping,
                                     ActionForm actionForm,
                                     HttpServletRequest httpServletRequest,
                                     HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;
        ActionMessages errors = form.validate(actionMapping, httpServletRequest);
        if (!errors.isEmpty()) {
            saveErrors(httpServletRequest, errors);
            actionMapping.findForward("viewCreateModel");
        }

        itemModelService.createModel(form.getMap());

        return actionMapping.findForward("viewModelIndex");

    }

    public ActionForward createModelPopup(ActionMapping actionMapping,
                                     ActionForm actionForm,
                                     HttpServletRequest httpServletRequest,
                                     HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;

        itemModelService.createModel(form.getMap());

        return null;
    }

    public ActionForward viewEditModel(ActionMapping actionMapping,
                                       ActionForm actionForm,
                                       HttpServletRequest httpServletRequest,
                                       HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;

        int id = Integer.parseInt(httpServletRequest.getParameter("modelId"));
        ItemModel model = itemModelService.getById(id);

        form.set("modelId", model.getModelId());
        form.set("manufacturerId", model.getManufacturer().getId());
        form.set("equipmentTypeId", model.getType().getId());
        form.set("modelName", model.getName());
        form.set("processorSpeed", model.getProcessorSpeed());
        form.set("screenSize", model.getScreenSize());

        return actionMapping.findForward("viewEditModel");

    }

    public ActionForward editModel(ActionMapping actionMapping,
                                   ActionForm actionForm,
                                   HttpServletRequest httpServletRequest,
                                   HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;
        ActionMessages errors = form.validate(actionMapping, httpServletRequest);
        if (!errors.isEmpty()) {
            saveErrors(httpServletRequest, errors);
            actionMapping.findForward("viewEditModel");
        }

        itemModelService.editModel(form.getMap());

        return actionMapping.findForward("viewModelIndex");

    }

    public ActionForward deleteModel(ActionMapping actionMapping,
                                     ActionForm actionForm,
                                     HttpServletRequest httpServletRequest,
                                     HttpServletResponse httpServletResponse) {

        DynaActionForm form = (DynaActionForm) actionForm;

        itemModelService.deleteModel(form.getMap());

        return actionMapping.findForward("viewModelIndex");

    }

}
